package com.thread.consumeandproduct;


import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

public class JetComManager {

    private static JetComManager instance = new JetComManager();

    private ConsumerJetCom consumerJetCom;
    private ProducerJetBlockQueue producerJetBlockQueue;
    private LinkedBlockingQueue<ProductJetBean> linkedBlockingQueue = new LinkedBlockingQueue<>();
    private boolean isContinueValid = false;

    public static JetComManager getInstance() {
        if (instance == null) {
            synchronized (JetComManager.class) {
                if (instance == null) {
                    instance = new JetComManager();
                }
            }
        }
        return instance;
    }

    public JetComManager() {
        consumerJetCom = new ConsumerJetCom();
        producerJetBlockQueue = new ProducerJetBlockQueue(consumerJetCom);
        MCLJETMain.executorService.execute(producerJetBlockQueue);
    }

    /**
     * 添加连续指令
     *
     * @param pjbl
     * @param isValidContinue
     * @return
     */
    public synchronized boolean productContinueList(List<ProductJetBean> pjbl, boolean isValidContinue) {
        if (linkedBlockingQueue.isEmpty()) {
            this.isContinueValid = isValidContinue;
            linkedBlockingQueue.addAll(pjbl);
            executeContinue();
            return true;
        }
        return false;
    }

    public void productMutil(ProductJetBean productJetBean) {
        producerJetBlockQueue.addMultiProduct(productJetBean);
    }

    /**
     * 添加互斥命令
     *
     * @param productJetBean
     * @return
     */
    public boolean productMutexSingle(ProductJetBean productJetBean) {
        if (producerJetBlockQueue.isAllowSingle() && linkedBlockingQueue.isEmpty()) {
            producerJetBlockQueue.addMultiProduct(productJetBean);
            return true;
        }
        return false;
    }

    /**
     * 连续访问信息
     */
    private synchronized void executeContinue() {
        ProductJetBean productJetBean = linkedBlockingQueue.poll();
        if (productJetBean != null) {
            productJetBean.setOnJetFinishListener(new OnJetFinishListener() {
                @Override
                public void timeOut(ProductJetBean productJetBean) {
                    System.out.println("continue timeOut:" + productJetBean.toString());
                    if (!isContinueValid) {
                        executeContinue();
                    } else {
                        System.out.println("continue timeOut intterupt:" + productJetBean.toString());
                        linkedBlockingQueue.clear();
                        executeContinue();
                    }
                }

                @Override
                public void finish(ProductJetBean productJetBean) {
                    System.out.println("continue finish:" + productJetBean.toString());
                    executeContinue();
                }
            });
            productMutil(productJetBean);
        }
    }


    public void receiveJet(String content) {
        producerJetBlockQueue.receive(content);
    }

}
